#import "LuPwdUtility.h"
#import "RSA.h"
#import "AllinHexUtil.h"
//#import "NSData+SecBase64.h"
#import "AllinBase64Util.h"
#define kRSAPublicKey @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+JONy3BsyljOmoBSnwCeXkV48Nj3W7hGTd71kUym35kRrSnGsX4eOvIcMbYv9PAa5LmxukzkLNBkqep5DCAAJF2FHP6wswKaKgosliajLcpwZFh6OJ/TA883pcB+v5I0rZZR3mQcq+mo/LXvb74ttdCnC0RWj5fcjRn1Xs6xpZwIDAQAB"

#define kMAXENCRYPTBLOCK    117

@implementation LuPwdUtility

+ (NSString*)rsaEncrypt:(NSString*)oriStr
{
    NSData *data = [LuPwdUtility conversePass:oriStr];
    NSString *retStr = [RSA encryptData:data publicKey:kRSAPublicKey padding:kSecPaddingPKCS1];
    retStr = [LuPwdUtility encode:retStr];
    return retStr;
}

+ (NSData*)conversePass:(NSString*)oriStr
{
    NSString *s = [[self class] hexStringFromString:oriStr];
    NSUInteger length = s.length/2;/*中文一个3字节，所以转成16进制后除以2才是真正的长度*/
    NSString *tenLength = [[self class] ToHex:(length/10 + 48)];
    NSString *digLength = [[self class] ToHex:(length%10 + 48)];
    NSString *targetEncode = [NSString stringWithFormat:@"%@%@%@",tenLength,digLength,s];
    
    
    if ((length + 2) > kMAXENCRYPTBLOCK) {
        NSLog(@"加密内容太长");
        return nil;
    }
    
    NSMutableData *tmpData = [NSMutableData data];
    for (int i = 0; i < (length + 2); i++) {
        Byte tmpByte = (Byte)strtoul([[targetEncode substringWithRange:NSMakeRange(i*2, 2)] UTF8String],0,16);
        [tmpData appendBytes:&tmpByte length:1];
    }
    
    for (NSInteger i = tmpData.length; i < kMAXENCRYPTBLOCK; i++) {
        Byte tmpByte = (Byte)(arc4random()*10%10);
        [tmpData appendBytes:&tmpByte length:1];
    }
    
    return [tmpData copy];
}

+ (NSString*)encode:(NSString*)oriStr
{
    NSString *retStr = nil;
    
    __block Byte *output = NULL;
    __block NSInteger length = 0;
    
    NSData *data = [AllinBase64Util decode:oriStr];
    
    [data enumerateByteRangesUsingBlock:^(const void * _Nonnull bytes, NSRange byteRange, BOOL * _Nonnull stop) {
        if (output == NULL) {
            length = byteRange.length * 2;
            output = (Byte*)malloc(length);
        }
        
        unsigned char *dataBytes = (unsigned char*)bytes;
        
        Byte encodingTable[16] = {48,49,50,51,52,53,54,55,56,57,97,98,99,100,101,102};
        
        for (NSInteger i = 0; i < byteRange.length; i++) {
            int v = (Byte)dataBytes[i]&0XFF;
            
            output[i*2] = encodingTable[v>>4];
            output[i*2+1] = encodingTable[v&0xf];
        }
    }];
    
    retStr = [[NSString alloc] initWithBytesNoCopy:output length:length encoding:NSUTF8StringEncoding freeWhenDone:YES];
    retStr = [retStr uppercaseString];
    
    NSUInteger gapLen = 256 - retStr.length;
    for (NSUInteger j = 0; j < gapLen; j = j + 1) {
        retStr = [@"0" stringByAppendingString:retStr];
    }
    
    return retStr;
}

+ (NSString *)ToHex:(uint16_t)tmpid
{
    NSString *nLetterValue;
    NSString *str =@"";
    uint16_t ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:
                nLetterValue = [NSString stringWithFormat:@"%u",ttmpig];
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
    }
    
    return str;
}

//普通字符串转换为十六进制的
+ (NSString *)hexStringFromString:(NSString *)string
{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

@end
